package com.xyz.basesdk.base;

import android.app.Activity;
import android.support.multidex.MultiDexApplication;
import android.util.ArrayMap;

import com.alibaba.android.arouter.launcher.ARouter;
import com.squareup.leakcanary.BuildConfig;
import com.squareup.leakcanary.LeakCanary;

/**
 * @author mac
 * on 2018/6/22.
 */
public class BaseApplication extends MultiDexApplication {

    public static BaseApplication mContext;
    public static String methodFlag;
    public static boolean isRemoveActivity = true;
    private static ArrayMap<String, Activity> activities = new ArrayMap<>();

    public static BaseApplication getContext() {
        return mContext;
    }

    public static void addActivity(String name, Activity activity) {
        isRemoveActivity = true;
        activities.put(name, activity);
    }

    public static void removeActivity(String activityName) {
        activities.remove(activityName);
    }

    public static void clearActivity() {
        isRemoveActivity = false;
        for (int i = 0; i < activities.size(); i++) {
            Activity activity = activities.valueAt(i);
            activity.finish();
        }
        activities.clear();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        initLeak();
        initARouter();
    }

    private void initARouter() {
        if(BuildConfig.DEBUG){
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(this);
    }

    private void initLeak() {
        LeakCanary.install(this);
    }

}
